//
//  CMMyIdentifierCtrl.m
//  CreditManger
//
//  Created by chaiyanan on 15/11/2.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "CMMyIdentifierCtrl.h"
#import "CMUserIdentityParam.h"
#import "CMUserIdentityParser.h"
#import "CMLoginManager.h"
#import "CheckValid.h"

@interface CMMyIdentifierCtrl ()

@property (nonatomic, weak) IBOutlet UITextField *nameTF;
@property (nonatomic, weak) IBOutlet UITextField *identifierTF;
@property (nonatomic, weak) IBOutlet UIButton *sureBtn;

@end

@implementation CMMyIdentifierCtrl

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.sureBtn.layer.cornerRadius = 8.0f;
    self.sureBtn.clipsToBounds = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - action

- (IBAction)clickToMakeSure:(UIButton *)sender
{
    if ([CheckValid checkStrIsEmpty:self.nameTF.text])
    {
        [self.nameTF becomeFirstResponder];
        DISSMISS_ERRView(@"用户名不能为空",[[[UIApplication sharedApplication] windows] lastObject]);
    }
    else if ([CheckValid checkStrIsEmpty:self.identifierTF.text])
    {
        [self.identifierTF becomeFirstResponder];
        DISSMISS_ERRView(@"身份证号不能为空",[[[UIApplication sharedApplication] windows] lastObject]);
    }
    else
    {
        [self.nameTF resignFirstResponder];
        [self.identifierTF resignFirstResponder];
        [self requestToCheckUserIdentity];
    }
}

- (IBAction)clickToEndEditing
{
    [self.nameTF resignFirstResponder];
    [self.identifierTF resignFirstResponder];
}

#pragma mark - request

- (void)requestToCheckUserIdentity
{
    CMUserIdentityParam *param = [[CMUserIdentityParam alloc]init];
    param.uid = [CMLoginManager sharedCMLoginManager].currentUserInfo.uid;
    param.token = [CMLoginManager sharedCMLoginManager].currentUserInfo.token;
    param.username = self.nameTF.text;
    param.cardno = self.identifierTF.text;
    
    CMUserIdentityParser *parser = [[CMUserIdentityParser alloc]init];
    [NetCenter postDataFromURL:Method_userIdentity params:param parser:parser result:^(ParserResult *myResult) {
        if (myResult.code==0)
        {
            [[NSNotificationCenter defaultCenter]postNotificationName:Notification_refreshUserInfo object:nil];
            DISSMISS_SUC(myResult.message);
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            DISSMISS_ERR(myResult.message);
        }
        
    } failure:^(NSError *error) {
        DISSMISS_ERR([error description]);
    }];
}


@end
